home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / Char.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  3.4 KB  |  111 lines  |  [TEXT/R*ch]

  1. (* Char -- new basis 1995-05-01, 1995-11-10 *)
  2.  
  3. type char = char
  4. (* Invariant: for c: char it holds that 0 <= ord c <= maxOrd *)
  5.  
  6. local 
  7.     prim_val sub_     : string -> int -> char = 2 "get_nth_char"
  8.     prim_val chr_     : int -> char           = 1 "identity";
  9.     prim_val length_  : string -> int         = 1 "string_length";
  10. in
  11.     prim_val ord : char -> int               = 1 "identity";
  12.     val minChar = #"\000"
  13.     val maxChar = #"\255"
  14.     val maxOrd = 255;
  15.  
  16.     fun chr i = if i<0 orelse i>maxOrd then raise Chr else chr_ i;
  17.  
  18.     fun succ c = 
  19.     if c < maxChar then chr_(ord c + 1) else raise Chr;
  20.     fun pred c = 
  21.     if c > minChar then chr_(ord c - 1) else raise Chr;
  22.  
  23.     local
  24.     prim_type array_
  25.     prim_val array_    : int -> array_ = 1 "create_string";
  26.     prim_val update_   : array_ -> char -> int -> unit 
  27.                    = 3 "set_nth_char";
  28.     prim_val fill_     : array_ -> int -> int -> int -> unit 
  29.                   = 4 "fill_string";
  30.     prim_val subarray_ : array_ -> char -> int = 2 "get_nth_char"
  31.  
  32.         fun mktable s =
  33.         let val len = length_ s
  34.         val table = array_ 256
  35.         fun init i = 
  36.             if i < len then (update_ table (sub_ s i) 1; init (i+1))
  37.             else ()            
  38.         in
  39.         fill_ table 0 256 0;
  40.         init 0;
  41.         table
  42.         end
  43.     in
  44.     fun contains s = 
  45.         let val table = mktable s 
  46.         in fn c => subarray_ table c = 1 end
  47.  
  48.     fun notContains s = 
  49.         let val table = mktable s 
  50.         in fn c => subarray_ table c = 0 end
  51.     end
  52.  
  53.     fun isLower c  = #"a" <= c andalso c <= #"z"
  54.     fun isUpper c  = #"A" <= c andalso c <= #"Z"
  55.     fun isDigit c  = #"0" <= c andalso c <= #"9"
  56.     fun isAlpha c  = isLower c orelse isUpper c
  57.     fun isHexDigit c = #"0" <= c andalso c <= #"9"
  58.                    orelse #"a" <= c andalso c <= #"f"
  59.                    orelse #"A" <= c andalso c <= #"F"
  60.     fun isAlphaNum c = isAlpha c orelse isDigit c
  61. (*    fun isPrint c  = c >= #" " andalso c <> #"\127" andalso c <> #"\255"  *)
  62.     fun isPrint c  = c >= #" " andalso c < #"\127" 
  63.     fun isSpace c  = c = #" " orelse #"\009" <= c andalso c <= #"\013"
  64.     fun isGraph c  = isPrint c andalso not (isSpace c)
  65.     fun isPunct c  = isGraph c andalso not (isAlphaNum c)
  66.     fun isAscii c  = c <= #"\127"
  67. (*    fun isCntrl c  = c < #" " orelse c = #"\127" orelse c = #"\255" *)
  68.     fun isCntrl c  = c < #" " orelse c >= #"\127"
  69.  
  70.     fun toLower c = 
  71.     if #"A" <= c andalso c <= #"Z" then chr_(ord c + 32)
  72.     else c;
  73.     fun toUpper c = 
  74.     if #"a" <= c andalso c <= #"z" then chr_(ord c - 32)
  75.     else c;
  76.  
  77.     fun toString c = Strbase.toMLescape c
  78.  
  79.     fun fromString s = 
  80.     let fun getc i = if i < size s then SOME (sub_ s i, i+1) else NONE
  81.     in 
  82.         case getc 0 of
  83.         NONE              => NONE
  84.           | SOME(#"\\", rest) => (case Strbase.fromMLescape getc rest of
  85.                       NONE       => NONE
  86.                     | SOME(c, _) => SOME c)
  87.           | SOME(c, _ )       => SOME c
  88.     end
  89.  
  90.     fun fromCString s = 
  91.     let fun getc i = if i < size s then SOME (sub_ s i, i+1) else NONE
  92.     in 
  93.         case getc 0 of
  94.         NONE              => NONE
  95.           | SOME(#"\\", rest) => (case Strbase.fromCescape getc rest of
  96.                       NONE       => NONE
  97.                     | SOME(c, _) => SOME c)
  98.           | SOME(c, _ )       => SOME c
  99.     end
  100.  
  101.     fun toCString c = Strbase.toCescape c;    
  102.  
  103.     fun compare (x, y: char) = 
  104.     if x<y then LESS else if x>y then GREATER else EQUAL;
  105.  
  106.     val op <  = op < : char * char -> bool;
  107.     val op <= = op <= : char * char -> bool;
  108.     val op >  = op >  : char * char -> bool;
  109.     val op >= = op >= : char * char -> bool;
  110. end
  111.